#include "client.h"

int func_puts_client(int netfd, char * filename)
{
    printf("文件不存在!\n");                                              
    //printf("i will send filename\n");
    train_t pTrain;
    int ret = access(filename, F_OK);
    //printf("filename=%s\n",filename);
    filename[strcspn(filename, "\n")] = '\0';
    if (ret == -1) {
        printf("文件不存在!\n");                                              
        return -1;
    }
    pTrain.length =strlen(filename);
    //printf("train filenamt-------------- len is %d\n",pTrain.length);
    //printf("train filename data is %s\n",pTrain.data);
    //memcpy(pTrain.data, filename, strlen(filename));///给traindata赋初值
    //send(netfd, pTrain, sizeof(pTrain.), 0);
    printf("train filenamt-------------- len is %d\n",pTrain.length);
    send(netfd,&pTrain.length, sizeof(pTrain.length), MSG_NOSIGNAL);
    memcpy(pTrain.data, filename, strlen(filename));///给traindata赋初值
    printf("train filename data is %s\n",pTrain.data);
    send(netfd, pTrain.data, pTrain.length, MSG_NOSIGNAL);
    //printf("文件名字的长度和文件名字本身我已经发过去了\n");
    int fd = open(filename, O_RDWR);
    if (fd == -1) {
        printf("open file is failed!\n");
        return -1;
    }
    while(1) {
        bzero(pTrain.data, sizeof(pTrain.data));
        ssize_t sret =read(fd, pTrain.data, sizeof(pTrain.data));
        pTrain.length = sret;
        send(netfd, &pTrain.length, sizeof(pTrain.length), MSG_NOSIGNAL);
        send(netfd, pTrain.data, pTrain.length, MSG_NOSIGNAL);
        if(sret == 0) {
            break;
        }

    }
    close(fd);
    return 0;
}


cheese@cheese-machine:~/study/test3/cloud-storage-project/wp-v4/client$ cat func_gets_client.c 
#include "client.h"

int func_gets_client(int netfd){
    sleep(2);
    printf("初始化等待完成\n");
    char ff[5]={0};                                        
    recv(netfd,ff,5,0);
    if(ff[0]=='b')
    {
        printf("失败\n");
        return -1;
    }
    train_t train;
    char filename[100]={0};
    recvn(netfd, &train.length, sizeof(train.length));
    recvn(netfd, train.data, train.length);
    memcpy(filename,train.data,train.length);
    int fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
    printf("收到文件名%s\n",filename);
    sleep(1);
    off_t filesize=0;
    recvn(netfd,&filesize,sizeof(off_t));
    printf("filesize = %ld\n", filesize);

    off_t cursize = 0;
    off_t lastsize = 0;
    off_t slice = filesize/10000;
    while(1) {
        if(cursize==filesize)break;
         recvn(netfd, &train.length, sizeof(train.length));
         if(train.length == 0) {
             break;
         }
         cursize += train.length;
         if(cursize - lastsize > slice) {
             printf("%5.2lf%%\r", 100.0*cursize/filesize);
             fflush(stdout);
             lastsize = cursize;
         }
         recvn(netfd, train.data, train.length);
         write(fd, train.data, train.length);
     }
     printf("success\n");
     close(fd);
     return 0;
}
